var matchProject = {
    addressParams:{},// 地址接口入参
    params:{
        parentCustomerCode:'',//渠道商
        customerCode:'',//客户编码
        pageNum:1,
        pageSize:20,
        provinceCode:'',//省
        cityCode:'',//市
        areaCode:'',// 区
        townCode:'',// 县
        detailAddress:'',//详细地址
    },//查询列表入参
    init:function(){
        this.addClick();
        this.getDataInfo();
        this.getLowerAddressInfo()
    },
    addClick:function(){
        // 所属渠道模糊查询
        $('#channelId').on('input',function(){
            var value = $('#channelId').val();
            matchProject.channelInfo(value)
            $('.channelLists').show();
        });
        // 获取所属渠道的某一项内容
        $('.channelLists').delegate('li','click',function(){
            var itemLi = $(this).text();
            var channelId = $(this).attr('data-id');
            matchProject.params.parentCustomerCode = channelId;
            $('#channelId').val(itemLi);
            $('.channelLists').hide();
        });
        // 客户名称
        $('#customerName').on('input',function(){
            if(!matchProject.params.parentCustomerCode){
                layer.msg('请选择所属渠道',{offset:'t'});
                return ;
            }
            var value = $('#customerName').val();
            matchProject.customerInfo(value)
            $('.customerLists').show();
        });
        // 客户名称某一项
        $('.customerLists').delegate('li','click',function(){
            var itemLi = $(this).text();
            $('#customerName').val(itemLi);
            matchProject.params.customerCode =  $(this).attr('data-id');
            $('.customerLists').hide();
        });
        $(document).on('click',function(){
            $('.channelLists').hide();
            $('.customerLists').hide();
        });
        // 一级地址
        $('#first').change(function(){
            var value = $('#first').val();
            matchProject.addressParams.level = '102';
            matchProject.addressParams.parentCode = value;
            matchProject.params.provinceCode = value;
            $('#third').html('<option value="">请选择三级地址</option>')
            $('#fourth').html('<option value="">请选择四级地址</option>')
            matchProject.getLowerAddressInfo();
        });
        // 二级地址
        $('#second').change(function(){
            var value = $('#second').val();
            matchProject.addressParams.level = '103';
            matchProject.addressParams.parentCode = value;
            matchProject.params.cityCode = value;
            $('#fourth').html('<option value="">请选择四级地址</option>')
            matchProject.getLowerAddressInfo();
        });
        // 三级地址
        $('#third').change(function(){
            var value = $('#third').val();
            matchProject.addressParams.level = '104';
            matchProject.addressParams.parentCode = value;
            matchProject.params.areaCode = value;
            matchProject.getLowerAddressInfo();
        });
        //四级地址
        $('#fourth').change(function(){
            var value = $('#fourth').val();
            matchProject.addressParams.parentCode = value;
            matchProject.params.townCode = value;
        });
        // 查询
        $('#searchButton').unbind().on('click',function(){
            matchProject.params.detailAddress = $('#detailAddress').val();
            matchProject.getDataInfo();
        });
        // 重置
        $('#resetButton').unbind().on('click',function(){
            matchProject.params.parentCustomerCode = '';
            matchProject.params.customerCode = '';
            matchProject.params.pageNum = 1;
            matchProject.params.pageSize = 20;
            matchProject.params.provinceCode = '';
            matchProject.params.cityCode = '';
            matchProject.params.areaCode = '';
            matchProject.params.townCode = '';
            matchProject.params.detailAddress = '';

        });
    },
    //列表查询
    getDataInfo:function(){
        var successData = function (data) {
            if (data.errorCode == 0) {
                if (data.data.result.length > 0 && !!data.data.result) {
                    matchProject.renderLists(data.data.result)
                    $(".M-box3").pagination({
                        pageCount: data.data.totalPage,
                        jump: true,
                        coping: true,
                        isHide: false,
                        prevContent: '上页',
                        nextContent: '下页',
                        callback: function (api) {
                            matchProject.params.pageNum = api.getCurrent()
                            var successData = function (data) {
                                matchProject.renderLists(data.data.result)
                            }
                            return web.doAjax(root.channelManagement.queryAddressConfigByPage, 'post', matchProject.params, successData, 1);
                        }
                    })
                } else {
                    $(".M-box3").pagination({
                        pageCount: 0
                    })
                    matchProject.renderLists(data.data.result)
                }
            } else {
                $.Huimodalalert('数据异常', 2000);
            }

        }
        return web.doAjax(root.channelManagement.queryAddressConfigByPage, 'post', matchProject.params, successData, 1)
    },
    renderLists: function (data) {
        var dataSource = [];
        $.each(data, function (index, item) {
            var obj = {};
            obj.index = index + 1// 序号
            obj.parentCustomerName = item.parentCustomerName; // 所属渠道
            obj.customerName = item.customerName;// 客户名称
            obj.fullAddress = item.fullAddress;// 地址信息
            obj.createdAt = item.createdAt;// 创建时间
            obj.operateUserName = item.operateUserName; // 操作人
            obj.parentCustomerCode = item.parentCustomerCode;//所属渠道唯一标识
            obj.customerCode = item.customerCode;//客户名称标识
            dataSource.push(obj)
        })
        matchProject.lists(dataSource)
    },
    lists: function (data) {
        var datatable = $("#datatable").dataTable(); // 初始化dataTable，清空上一个值
        datatable.fnClearTable();//清空一下table
        datatable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[1, "desc"]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据"
            },
            data: data,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" >' + row.index + '</span>';
                    },
                    "targets": 0
                },
                // 所属渠道
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" title="'+row.parentCustomerName+'">' + row.parentCustomerName + '</span>';

                    },
                    "targets": 1
                },
                // 客户名称
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" title="'+row.customerName+'">' + row.customerName + '</span>';
                    },
                    "targets": 2
                },
                //地址信息
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" title="'+row.fullAddress+'">' + row.fullAddress + '</span>';
                    },
                    "targets": 3
                },
                // 创建时间
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;">' + row.createdAt + '</span>';
                    },
                    "targets": 4
                },
                // 操作人
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;">' + row.operateUserName + '</span>';
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var obj = {
                            parentCustomerCode:row.parentCustomerCode,
                            customerCode:row.customerCode
                        }
                        var newObj = JSON.stringify(obj)
                        return "<span style='text-align: center; color: blue; cursor: pointer;margin-right: 20px' onclick='matchProject.matchProjectEdit("+newObj+")'>编辑</span>"+
                            "<span style='text-align: center; color: blue; cursor: pointer' onclick='matchProject.matchProjectCancel("+newObj+")'>删除</span>"
                    },
                    "targets": 6
                },
            ],
        })
    },
    // 编辑
    matchProjectEdit:function(obj){
        var parentCustomerCode = obj.parentCustomerCode;
        var customerCode = obj.customerCode;
        var url = layer.open({
            type: 2,
            title: '编辑',
            content: 'editMatchProject.html?channelId='+parentCustomerCode +'&customerCode='+ customerCode
        });
        layer.full(url);
    },
    // 删除
    matchProjectCancel:function(obj){
        layer.confirm('请确认要删除该条数据，删除后则无法对应项目关系，将影响订单生产!',{
            btn: ['确认', '取消'], //按钮
            title:'删除提示',
            offset:"t"
        },function(index){
            var params = {
                parentCustomerCode:obj.parentCustomerCode,
                customerCode:obj.customerCode,
                operateUserId:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
                operateUserName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
            }
            matchProject.deleteAddressInfo(params)
            layer.close(index)

        })
    },
    // 地址查询
    getLowerAddressInfo:function(){
        var successCallBack = function(data){
            if(data.errorCode == 0){
                var result = data.list;
                if(matchProject.addressParams.level == '102'){
                    matchProject.renderSecondAddress(result)
                }else if(matchProject.addressParams.level == '103'){
                    matchProject.renderThirdAddress(result)
                }else if(matchProject.addressParams.level == '104'){
                    matchProject.renderFourthAddress(result)
                }else{
                    matchProject.renderFirstAddress(result)
                }

            }
        }
        return  web.doAjax(root.member.getLowerAddress,'post',matchProject.addressParams, successCallBack);
    },
    // 渲染一级地址
    renderFirstAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择一级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#first').html(str)
        }else{
            var emptyStr = '<option value="">暂无地址</option>';
            $('#first').html(emptyStr)
        }
    },
    // 渲染二级地址
    renderSecondAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择二级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#second').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#second').html(emptyStr)
        }
    },
    // 渲染三级地址
    renderThirdAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择三级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#third').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#third').html(emptyStr)
        }
    },
    // 渲染四级地址
    renderFourthAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择四级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#fourth').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#fourth').html(emptyStr)
        }
    },
    // 所属渠道模糊查询接口
    channelInfo:function(value){
        var params = {
            name:value,
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result && result.length >0){
                    var channelItem =  '';
                    result.forEach(function(item,index){
                        channelItem +="<li class='clearfix pl-10 mb-5' data-id='"+item.customerCode+"'>"+item.customerName+"</li>"
                    });
                    $('.channelLists').html(channelItem)
                }else{
                    var empty = '<li class="clearfix pl-10 mb-5">无数据</li>';
                    $('.channelLists').html(empty)
                }
            }
        }
        return web.doAjax(root.channelInovice.getChannelConfigByChannelName, 'post', params, successData, 2);
    },
    // 客户名称
    customerInfo:function(value){
        var params = {
            customerName:value,
             parentCustomerCode:matchProject.params.parentCustomerCode
            /*parentCustomerCode:'D000000022'*/
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result && result.length >0){
                    var channelItem =  '';
                    result.forEach(function(item,index){
                        channelItem +="<li class='clearfix pl-10 mb-5' data-id='"+item.customerCode+"'>"+item.customerName+"</li>"
                    });
                    $('.customerLists').html(channelItem)
                }else{
                    var empty = '<li class="clearfix pl-10 mb-5">无数据</li>';
                    $('.customerLists').html(empty)
                }
            }else{
                layer.msg(data.msg,{offset:'t'});
            }
        }
        return web.doAjax(root.dockingplatform.seachSubChanelConfigByName, 'post', params, successData, 1);
    },

    // 删除接口
    deleteAddressInfo:function(params){
      var successData = function(data){
            if(data.errorCode == 0){
                layer.msg('删除成功',{offset:'t'});
                matchProject.getDataInfo()
            }else{
                layer.msg('删除失败',{offset:'t'});
            }
      }
        return web.doAjax(root.channelManagement.deleteAddressConfig, 'post', params, successData, 1);
    }


}
matchProject.init();